const express = require('express')
const {operationalDatabase} = require('./mysql')
const app = express()
//post请求接收参数设置
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
//跨域设置
app.all('*', function (req, res, next) {
	res.header('Access-Control-Allow-Origin', '*');
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	res.header('Access-Control-Allow-Methods', '*');
	res.header('Content-Type', 'application/json;charset=utf-8');
	next();
});
//获取分类数据
app.get('/login/:username',(req,result)=>{
	const username = req.params.username
	console.log(username);
	const sql = 'select * from user where username = '+'"'+username+'"'
	operationalDatabase(sql,res=>{
		result.send(res);
	})	
})
app.get('/classity',(req,result)=>{
	const sql = 'select * from classity'
	operationalDatabase(sql,res=>{
		result.send(res);
	})
})
//添加分类
app.post('/class/add',(req,result)=>{
	console.log(req.body.title)
	const valueCon = req.body.text
	const sql = `insert into classity(text) value('${valueCon}');`
	operationalDatabase(sql,res=>{
		result.send(res);
	})
})
// 修改分类
app.post('/class/update',(req,result)=>{
	const idCont = req.body.id;
	const textCont = req.body.text
	const sql = `update classity set text='${textCont}' where value = '${idCont}';`
	operationalDatabase(sql,res=>{
		result.send(res);
	})
})
//删除分类
app.post('/class/delete',(req,result)=>{
	const idCont = req.body.id;
	const sql = `delete from classity where value ='${idCont}' `
	operationalDatabase(sql,res=>{
		result.send(res);
	})
})
app.listen(3000,()=>{
	console.log('服务已启动...');
})